package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Members;
import com.itheima.pojo.User;
import com.itheima.service.OrderSettinglistService;
import com.itheima.utils.GetAgeUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("/ordersettinglist")
@RestController
public class OrderSettinglistConterller {
    @Reference
    private OrderSettinglistService orderSettinglistService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderSettinglistService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Members member) {
        Integer id = member.getId();
        Date birthday = member.getBirthday();
        System.out.println(birthday);
        String idCard = member.getIdCard();
        String evaluate = GetAgeUtil.evaluate(idCard);
        member.setAge(evaluate);
        try {
            if (id == null) {
                orderSettinglistService.add(member);
            }else{
                orderSettinglistService.update(member);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "凉");
        }
        return new Result(true, "成");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Members member = orderSettinglistService.findById(id);
            return new Result(true,"成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderSettinglistService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
        return new Result(true,"成功");
    }
    @RequestMapping("/findOrder")
    public Result findOrder() {
        try {
            List<User> list = orderSettinglistService.findOrder();
            return new Result(true,"成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
}
